package com.hashnode.testservice;

import cn.hutool.core.util.IdUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.concurrent.TimeUnit;

@Service
public class TestPaymentService {
//    正常访问
    public String paymentInfo_OK(Integer id){
        return "线程池:" + Thread.currentThread().getName() + "  paymentInfo_OK,id" + id +"\t";
    }

    // 服务降级
//    运行缓慢或出错则会进行服务降级
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000")
    })
    public String paymentInfo_TimeOut(Integer id){
        try {
            //运行异常
//            int a = 1/0;
            int timeNumer = 3;
            TimeUnit.SECONDS.sleep(timeNumer);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "线程池:" + Thread.currentThread().getName() + "  paymentInfo_TimeOut,id" + id +"\t";
    }

    //服务降级，兜底方案
    public String paymentInfo_TimeOutHandler(Integer id){
        return "线程池:" + Thread.currentThread().getName() + "error paymentInfo_TimeOutHandler,id" + id +"\t";
    }


}
